<?php

include_once($ROOT . "base.inc.php");

class PageController extends BaseController {

  var $event;
  var $contact;
  var $contactAddress;
  var $location;
  var $locationAddress;
  var $events;

  var $contacts;
  var $locations;

  function PageController() {
	parent::__construct();

	$this->title = "Events";
	$this->section = "events";

	$action = $this->getParameter("action");
	$this->event = new Event();
	$this->contact = new Contact();
	$this->contactAddress = new Address();
	$this->location = new Location();
	$this->locationAddress = new Address();

	switch (strtoupper($action))
	{
		case "SAVE" :
			  $this->parseForm();
			  if (!$this->validate()) break;

			  if ($this->contact != null)
			  {
			  	 if ($this->contactAddress != null)
			  	 {
			  	 	if (!AddressLogic::save($this->contactAddress))
			  	 	{
			  	 		$this->addError("Could not save Contact Address");
			  	 		break;
			  	 	}
			  	 	$this->contact->address_id = $this->contactAddress->id;
			  	 }

			  	 if (!ContactLogic::save($this->contact))
			  	 {
			  	 	$this->addError("Could not save Contact");
			  	 	break;
			  	 }
			  	 $this->event->contact_id = $this->contact->id;
			  }

			  if ($this->location != null)
			  {
			  	 if ($this->locationAddress != null)
			  	 {
			  	 	if (!AddressLogic::save($this->locationAddress))
			  	 	{
			  	 		$this->addError("Could not save Location Address");
			  	 		break;
			  	 	}
			  	 	$this->location->address_id = $this->locationAddress->id;
			  	 }

			  	 if (!LocationLogic::save($this->location))
			  	 {
			  	 	$this->addError("Could not save Location");
			  	 	break;
			  	 }
			  	 $this->event->location_id = $this->location->id;
			  }

			  if (!EventLogic::save($this->event))
			  {
			  	  $this->addError("Could not save Event");
			  	  break;
			  }

			  $this->addMessage("Saved");
			  break;
		case "LOAD" :
			  $id = $this->getParameter("id");
	  		  $this->event = EventLogic::load($id);
	  		  break;
	    case "CANCEL" :
	  		  HtmlUtil::redirectToPage("searchEvents");
	  	default :
	}

	$this->events = EventLogic::findAll();
	$this->contacts = ContactLogic::findAll();
	$this->locations = LocationLogic::findAll();

  }

  function parseForm() {
  	$this->event->pubilc = "F";
  	$this->event->readForm();
  	$startDate = $this->getParameter("startDate");
  	$startTime = $this->getParameter("startTime");
  	$endDate = $this->getParameter("startDate");
  	$endTime = $this->getParameter("endTime");

  	if ($startDate != null || $endTime != null)
  	{
  		$this->event->start = date("Y-m-d H:i:s", strToTime($startDate . " " . $startTime));
  	}
  	if ($endDate != null || $endTime != null)
  	{
  		$this->event->end = date("Y-m-d H:i:s", strToTime($endDate . " " . $endTime));
  	}

	if ($this->getParameter("contact_modified") == 1)
	{
  		$this->contact->readForm("contact_");
	  	if ($this->getParameter("contact_address_modified") == 1)
	  	{
	  		$this->contactAddress->readForm("contact_address_");
	  	}
	  	else
	  	{
	  		$this->contactAddress = null;
	  	}
	}
	else
	{
		$this->contact = null;
		$this->contactAddress = null;
	}

	if ($this->getParameter("location_modified") == 1)
	{
  		$this->location->readForm("location_");
  		if ($this->getParameter("address_modified"))
  		{
  			$this->locationAddress->readForm("address_");
  		}
  		else
  		{
  			$this->locationAddress = null;
  		}
	}
	else
	{
		$this->location = null;
		$this->locationAddress = null;
	}
  }

  function validate() {
	return (sizeof($this->errs) == 0);
  }

}



?>
